/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.example.aop;

import org.aopalliance.aop.Advice;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodMatcher;
import org.springframework.aop.Pointcut;
import org.springframework.aop.PointcutAdvisor;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.example.aop.advice.MyBeforeAdvice;
import org.springframework.example.aop.service.AccountService;

import java.lang.reflect.Method;

/**
 * @author: baiml
 * @Title: Test4
 * @ProjectName: 作业管理平台
 * @Depart 软件事业部
 * @Description:
 * @date: 2021/10/12 12:33
 */
public class Test4 {

	public static void main(String[] args) {

		AccountService target = new AccountService();
		ProxyFactory proxyFactory = new ProxyFactory();
		proxyFactory.setTarget(target);
		proxyFactory.addAdvisor(new PointcutAdvisor(){
			@Override
			public Pointcut getPointcut() {
				return new Pointcut() {
					@Override
					public ClassFilter getClassFilter() {
						return ClassFilter.TRUE;
					}

					@Override
					public MethodMatcher getMethodMatcher() {
						return new MethodMatcher() {
							@Override
							public boolean matches(Method method, Class<?> targetClass) {
								return method.getName().equals("test");
							}

							@Override
							public boolean isRuntime() {
								return true;
							}

							@Override
							public boolean matches(Method method, Class<?> targetClass, Object... args) {
								return args.length == 2;
							}
						};
					}
				};
			}

			@Override
			public Advice getAdvice() {
				return new MyBeforeAdvice();
			}

			@Override
			public boolean isPerInstance() {
				return false;
			}
		});
		AccountService proxy = (AccountService) proxyFactory.getProxy();
		proxy.test("2","5");
	}
}
